$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'base/readerRelation/list',
        datatype: "json",
        colModel: [			
			{ label: '序号', name: 'id', index: 'ID', width: 50, key: true },
			{ label: '读写器', name: 'readerId', index: 'READERID', width: 80 }, 			
			{ label: '类型', name: 'type', index: 'TYPE', width: 80 }, 			
			{ label: '使用者', name: 'machinicid', index: 'MACHINICID', width: 80 }, 			
			{ label: '创建人', name: 'cuser', index: 'CUSER', width: 80 }, 			
			{ label: '创建时间', name: 'ctime', index: 'CTIME', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		readerRelation: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.readerRelation = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
            
		},
		saveOrUpdate: function (event) {
			vm.readerRelation.readerId = $("#creaderId").val();
			vm.readerRelation.type = $("#ctype").val();
			vm.readerRelation.machinicid = $("#cmachinicid").val();
			
			var url = vm.readerRelation.id == null ? "base/readerRelation/save" : "base/readerRelation/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.readerRelation),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "base/readerRelation/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "base/readerRelation/info/"+id, function(r){
                vm.readerRelation = r.readerRelation;
                $("#creaderId option").each(function () {
		            var optionVal = $(this).val();
		            var optionText = $(this).text();
		            //console.log(optionVal +" " + optionText);
		            if(optionText == vm.readerRelation.readerId )
		            {
		            	$("#creaderId").val(optionVal)
		            }
		        });
                
                $("#ctype option").each(function () {
		            var optionVal = $(this).val();
		            var optionText = $(this).text();
//		            console.log(optionVal +" " + optionText);
		            if(optionText == vm.readerRelation.type )
		            {
		            	$("#ctype").val(optionVal)
		            }
		        });
//                console.log(vm.readerRelation);
//                console.log(vm.readerRelation.type);
                vm.getCUser(vm.readerRelation.type);
                
                
            });
		},
		getCUser: function(type){
			var url = "";
			if (type=='班组')
			{
				 url = "base/group/qlist?page=1&limit=100";
				 $.get(baseURL + url, function(r){
					   $("#cmachinicid").empty();
					   for(var i in r.groupList){  
				    	   $("#cmachinicid").append('<option value='+r.groupList[i].id+'>' + r.groupList[i].name + '</option>');
				    	}
					 
		                $("#cmachinicid option").each(function () {
				            var optionVal = $(this).val();
				            var optionText = $(this).text();
				            if(optionText == vm.readerRelation.machinicid )
				            {
				            	$("#cmachinicid").val(optionVal)
				            }
				        });
		            });
			}
			else if(type=='龙门吊'){
				url = "base/longmendiao/qlist?page=1&limit=100";
				 $.get(baseURL + url, function(r){
					   $("#cmachinicid").empty();
					   for(var i in r.longmendiao){  
				    	   $("#cmachinicid").append('<option value='+r.longmendiao[i].id+'>' + r.longmendiao[i].neme + '</option>');
				    	}
					 
		                $("#cmachinicid option").each(function () {
				            var optionVal = $(this).val();
				            var optionText = $(this).text();
				            if(optionText == vm.readerRelation.machinicid )
				            {
				            	$("#cmachinicid").val(optionVal)
				            }
				        });
		            });
			}
			else if(type=='车辆')
			{
				url = "base/vehicle/qlist?page=1&limit=100";
				 $.get(baseURL + url, function(r){
					   $("#cmachinicid").empty();
					   for(var i in r.vehicle){  
				    	   $("#cmachinicid").append('<option value='+r.vehicle[i].id+'>' + r.vehicle[i].vehicle + '</option>');
				    	}
					 
		                $("#cmachinicid option").each(function () {
				            var optionVal = $(this).val();
				            var optionText = $(this).text();
				            if(optionText == vm.readerRelation.machinicid )
				            {
				            	$("#cmachinicid").val(optionVal)
				            }
				        });
		            });
			}
		},
		reloadCmachinic: function (event) {
			var optionVal = $("#ctype").val();
			vm.getCUser(optionVal);
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});

$(function(){ 
	$.ajax({
		type: "GET",
	    url: baseURL + 'base/htreaders/qlist'+'?page=1&limit=100',
        contentType: "application/json",
	    data: '',
	    success: function(r){
	    	for(var i in r.htreaders){  
	    	   $("#creaderId").append('<option value='+r.htreaders[i].readerid+'>' + r.htreaders[i].readerName + '</option>');
	    	}
		}
	});
	}
);
	
	
		
	　　

